package com.perfma.xtest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;

/**
 * @author: junhui.si
 * @date: 2021-03-29 14:52
 **/
public class PicDemo {
    static CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    public static void main(String[] args) throws Exception {
        Set<String> picList = Sets.newHashSet();
        Map<String, String> paramMap = Maps.newHashMap();
        paramMap.put("v", "2");
        JSONObject str = get("https://img1.maka.im/user/8906087/event/D9GSK54SW8906087/D9GSK54SW8906087_v240.json", paramMap, Maps.newHashMap());
        JSONArray array = (JSONArray) JSONPath.eval(str, "$.data.pdata.json");
        array.forEach(item -> {
            String itemStr = item.toString();
            JSONObject itemObject = JSONObject.parseObject(itemStr);
            JSONArray contentArray = itemObject.getJSONArray("content");
            contentArray.forEach(son -> {
                String sonStr = son.toString();
                JSONObject sonObject = JSONObject.parseObject(sonStr);
                picList.add(sonObject.getString("pcEditorPicId"));
            });
        });

        int i = 0;
        for (String s : picList) {
            try {
                download(s, i);
            } catch (Exception e) {
                e.printStackTrace();
            }
            i++;
        }
    }

    static void download(String uri, int i) throws Exception {
        URL url1 = new URL("https://img1.maka.im/" + uri);
        URLConnection uc = url1.openConnection();
        InputStream inputStream = uc.getInputStream();

        FileOutputStream out = new FileOutputStream("/Users/sijunhui/Downloads/pic/" + i + ".png");
        int j;
        while ((j = inputStream.read()) != -1) {
            out.write(j);
        }
        inputStream.close();
    }

    static JSONObject get(String url, Map<String, String> paramMap, Map<String, String> headerMap) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);

        if (CollectionUtil.isNotEmpty(paramMap)) {
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                uriBuilder.addParameter(entry.getKey(), entry.getValue());
            }
        }

        HttpGet httpGet = new HttpGet(uriBuilder.build());
        if (CollectionUtil.isNotEmpty(headerMap)) {
            for (Map.Entry<String, String> entry : headerMap.entrySet()) {
                httpGet.addHeader(entry.getKey(), entry.getValue());
            }
        }

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                // 使用Apache提供的工具类进行转换成字符串
                String entityStr = EntityUtils.toString(entity, "UTF-8");

                return JSONObject.parseObject(entityStr);
            }
        } finally {
            // 释放资源
            if (response != null) {
                response.close();
            }
        }
        return null;
    }
}
